/**
 * Created by leo on 2/2.
 */
angular.module('panels')
    .directive('panelBomMyDevicesStatus', function (loadConf, $http, tabService) {
        return {
            scope: {},
            link: function (scope) {
                loadConf('panel-bom-my-devices-status.yml').then(function (setting) {
                    scope.more = setting.more;
                    scope.types = setting.types;
                    scope.changeType = function (index) {
                        scope.types.currentOn = index;
                        $http.jsonp(setting.listQueryUrl, {
                            params: {
                                devType: scope.types[index].id.split(','),
                                rowCount: 10
                            }
                        }).success(function (rows) {
                            scope.rows = rows;
                            var bzTypes = [];
                            angular.forEach(rows, function (row) {
                                this.push(row.BZ_TYPE);
                            }, bzTypes);
                            $http.jsonp(setting.kbpNamesUrl, {
                                params: {
                                    kbpClass: bzTypes
                                },
                                cache: true
                            }).success(function (data) {
                                scope.bzTypeNames = data;
                            });
                        });
                    };
                    scope.changeType(0);
                    scope.getBzTypeName = function (bzType) {
                        return scope.bzTypeNames ? scope.bzTypeNames[bzType] || bzType : bzType;
                    };

                    // 打开综合视图
                    scope.openIntegrateView = function (row) {
                        tabService.addTab({
                            name: row.IP_ADDR,
                            url: setting.integrateViewUrl.replace('{UNIT_ID}', row.UNIT_ID)
                        });
                    };
                    // 打开告警视图
                    scope.openAalrmView = function (row) {
                        tabService.addTab({
                            name: row.IP_ADDR,
                            url: setting.alarmViewUrl.replace('{UNIT_ID}', row.UNIT_ID)
                        });
                    };
                });
            },
            templateUrl: 'app/panel/bom/my-devices-status.html'
        };
    });